/**
 * Created by Administrator on 2015/1/21.
 */
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var config = require('./config');
var Config = config.Config();
var bodyParser = require('body-parser');
var app = express();

var view = require('./view-init');
var utils = require('./utils');

exports.init = function () {
// view engine setup
    app.set('views', path.join(__dirname, '../views'));
    app.set('view engine', 'ejs');
    app.use(favicon(path.join(__dirname,Config.favicon)));
    logger.token('date', function(){
        return utils.getNowTimeStr();
    });
    app.use(logger(Config.logger));
    app.use(bodyParser.text({ type: Config.paraserType }));
    app.use(cookieParser());
    app.use(express.static(path.join(__dirname, '../public')));

    //init view
    view.viewInit(app);

// catch 404 and forward to error handler
    app.use(function (req, res, next) {
        config.code404(req, res, next)
    });
//error handler
    app.use(function (err, req, res, next) {
        config.errorHandler(err, req, res, next)
    });

    app.listen(Config.port, Config.host, function () {
        console.log('start server  on port ' + Config.port);
        console.log('url is :  http://' + Config.host + ':' + Config.port + '');
    });
}